package com.gz.purchase.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.gz.purchase.library.pojo.StorageCheckProductInfo;

import java.util.List;
import com.gz.purchase.R;
import com.gz.purchase.util.ToastUtils;


/**
 * 出库车出库界面 商品列表适配器
 * author wanner1991
 */
public class OutStorageCartAdapter extends BaseAdapter {
    private Context mContext;
    private List<StorageCheckProductInfo> mDataList;

    public OutStorageCartAdapter(Context mContext, List<StorageCheckProductInfo> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.out_storage_product_item,null);
            holder = new ViewHolder() ;
            holder.tv_product_name = (TextView) view.findViewById(R.id.tv_product_name);
            holder.tv_product_unit = (TextView) view.findViewById(R.id.tv_product_unit);
            holder.et_product_num = (EditText) view.findViewById(R.id.et_product_num);
            holder.tv_product_shop_name = (TextView) view.findViewById(R.id.tv_product_shop_name);

            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        final  StorageCheckProductInfo info  = mDataList.get(i);
        holder.tv_product_name.setText(info.getProduct_name());
        holder.tv_product_unit.setText("(剩余"+info.getProduct_num()+")"+"1" + info.getProduct_unit()+"*");
        holder.et_product_num.setText(info.getSelectNum()+ "");
        holder.tv_product_shop_name.setText(info.getShop_name());

        holder.et_product_num.setTag(info);
        holder.et_product_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                StorageCheckProductInfo info1 = (StorageCheckProductInfo) holder.et_product_num.getTag();
//                if (info1.getProduct_num() <= 0){
//                    ToastUtils.showToast(mContext,"剩余量为0，禁止编辑数量");
//                    return;
//                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String num = s.toString();
                StorageCheckProductInfo info2 = (StorageCheckProductInfo) holder.et_product_num.getTag();
                if (TextUtils.isEmpty(num)) { // 如果用户什么也不填写，则还原到修改之前的数据
                    info2.setSelectNum(0);
                    //holder.et_product_num.setText(info2.getSelectNum()+"");
                    return;
                }else if(Integer.parseInt(num) == info2.getSelectNum()){
                    return;
                }else if(Integer.parseInt(num) > info2.getProduct_num()){// 如果修改的数超过剩余量,则修改为剩余量
                    info2.setSelectNum(info2.getProduct_num());
                    holder.et_product_num.setText(info2.getProduct_num()+"");
                    return ;
                }
                // 否则修改数据
                info2.setSelectNum(Integer.parseInt(num));
                //holder.et_product_num.setText(num);

            }
        });
        return view;
    }

    class ViewHolder{
        TextView tv_product_name;
        TextView tv_product_unit;
        EditText et_product_num;
        TextView tv_product_shop_name;
    }
}
